package com.benshou.bsoa.dto.yida.analysis;


import org.springframework.stereotype.Component;


/*
 * 项目基础信息的转换->对象
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.benshou.bsoa.constants.ProjectAccountFieldConstants.*;

@Component
public class TransFormDataJsonToProject {
    // 提取FormData中的相关字段并映射到实体类
    public ProjectAccount mapFormDataToEntity(Map<String, ?> formData) {
        ProjectAccount entity = new ProjectAccount();

        // 项目信息
        entity.setProjectStatus((String) formData.get(PROJECT_STATUS));
        entity.setProjectName((String) formData.get(PROJECT_NAME));
        entity.setProjectCode((String) formData.get(PROJECT_CODE));
        entity.setProjectType((String) formData.get(PROJECT_TYPE));
        entity.setProjectLevel((String) formData.get(PROJECT_LEVEL));

        // 项目计划开始与结束日期
        entity.setPlanStartDate((Long) formData.get(PLAN_START_DATE));
        entity.setPlanStartDate((Long) formData.get(PLAN_END_DATE));

        // 部门信息
        entity.setSolutionDept(String.valueOf(formData.get(SOLUTION_DEPT)));
        List<String> solutionDeptIds = (List<String>) formData.get(SOLUTION_DEPT_IDS);
        entity.setSolutionDeptIds(solutionDeptIds);

        // 项目经理
        entity.setProjectManager(String.valueOf(formData.get(PROJECT_MANAGER)));
        List<String> managerIds = (List<String>) formData.get(USER_IDS);
        entity.setUserIds(managerIds);

        // 项目预算
        entity.setBudgetCost((Long) formData.get(BUDGET_COST));

        // 合同信息
        entity.setContractCompany((String) formData.get(CONTRACT_COMPANY));
        entity.setContractName((String) formData.get(CONTRACT_NAME));
        entity.setContractAmount((Long) formData.get(CONTRACT_AMOUNT));

       // 合同开始与结束日期
        entity.setContractStartDate((Long) formData.get(CONTRACT_START_DATE));
        entity.setContractEndDate((Long) formData.get(CONTRACT_END_DATE));

        // 客户信息
        entity.setCustomerName((String) formData.get(CUSTOMER_NAME));
        entity.setDemandDept((String) formData.get(DEMAND_DEPT));

        // 成员信息
        List<Map<String, Object>> members = (List<Map<String, Object>>) formData.get(PROJECT_MEMBER_TABLE);
        List<ProjectMember> projectMemberList = new ArrayList<>();

        if (members != null) {
            for (Map<String, Object> member : members) {
                ProjectMember projectMember = new ProjectMember();
                projectMember.setProjectName(entity.getProjectName());
                projectMember.setTotalMembers(members.size());
                projectMember.setUserId((List<String>) member.get(MEMBER_ID));
                projectMemberList.add(projectMember);
                //入项时间和离项时间
                //projectMember.setCostDate((Long) member.get(MEMBER_JOIN_DATE));
                //projectMember.setSalaryCost((Long) member.get(MEMBER_LEAVE_DATE));
                //关键角色
                //projectMember.setKeyRole((String) member.get(MEMBER_KEY_ROLE));
            }
        }
        entity.setMembers(projectMemberList);
        return entity;
    }
}
